package com.goodflower.goodflower.chang_user;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.goodflower.goodflower.FlowerActivity;
import com.goodflower.goodflower.R;
import com.goodflower.goodflower.info.Info;
import com.goodflower.goodflower.util.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Change_user_phone extends AppCompatActivity {
    private EditText change_user_phone_new;
    private Button btn_change_user_phone_ok_next;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_user_phone);
        //找到控件
        change_user_phone_new = (EditText)findViewById(R.id.change_user_phone_new);
        btn_change_user_phone_ok_next = (Button)findViewById(R.id.btn_change_user_phone_ok_next);
        //按钮点击事件
       btn_change_user_phone_ok_next.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               final String new_phone = change_user_phone_new.getText().toString();
               if(new_phone==null||new_phone.isEmpty()){
                   Toast.makeText(Change_user_phone.this,"请输入新的手机号码",Toast.LENGTH_LONG).show();
               }else {
                   Info.current_user_phone = new_phone;
                   HttpUtil.sendOkHttpRequest("http://"+Info.server_ip+"/huahua/phone_changuserphone.php?username="+ Info.current_user_name+"&phone="+new_phone, new Callback() {
                       @Override
                       public void onFailure(Call call, IOException e) {

                       }

                       @Override
                       public void onResponse(Call call, Response response) throws IOException {

                       }
                   });

                   runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                                   Toast.makeText(Change_user_phone.this,"设置成功",Toast.LENGTH_LONG).show();
                                   Intent intent = new Intent(Change_user_phone.this, FlowerActivity.class);
                                   intent.putExtra("yourname",Info.current_user_name);
                                   startActivity(intent);
                       }
                   });
               }
           }
       });
    }
}
